/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.member.handler;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.imoney.framework.base.entity.IRequest;
import com.imoney.framework.base.entity.Response;
import com.imoney.framework.base.exception.ServiceException;
import com.imoney.framework.base.utils.StringUtils;
import com.imoney.payment.chain.HandlerChain;
import com.imoney.payment.chain.IHandler;
import com.imoney.payment.member.common.InfoCode;
import com.imoney.payment.member.domain.Member;
import com.imoney.payment.member.domain.RegCompleteRequest;
import com.imoney.payment.member.domain.RegRequest;
import com.imoney.payment.member.enums.AccStatus;
import com.imoney.payment.member.service.IMemberService;

/**
 * 类说明：注册检查参数<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-6-6		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-6-6
 */
@Service
public class RegParamHandler implements IHandler<Member> {

	@Resource
	private IMemberService memberService;

	@Override
	public void doHandler(HandlerChain<Member> chain, IRequest request,
			Response<Member> response) throws ServiceException {
		Member member = null;

		/** 验证账户名 - 注册 */
		if (request instanceof RegRequest) {
			RegRequest regRequest = (RegRequest) request;
			response.setCode(InfoCode.PARAM_IS_NULL);
			if (StringUtils.isEmpty(regRequest.getLoginId())) {
				response.setMessage("loginId is null!");
				return;
			}
			if (null == regRequest.getCountry()) {
				response.setMessage("country is null!");
				return;
			}
			if (null == regRequest.getUserType()) {
				response.setMessage("userType is null!");
				return;
			}
			member = memberService.getObjectByName(regRequest.getLoginId());
			if (null != member) {
				response.setCode(InfoCode.MEMBER_LOGINID_EXIST);
				response.setMessage(member.getStatus().name());
				return;
			}
		}
		/** 填写账户信息 - 注册 */
		else if (request instanceof RegCompleteRequest) {
			RegCompleteRequest regRequest = (RegCompleteRequest) request;
			response.setCode(InfoCode.PARAM_IS_NULL);
			if (StringUtils.isEmpty(regRequest.getLoginId())) {
				response.setMessage("loginId is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getQueryPwd())) {
				response.setMessage("queryPwd is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getPayPwd())) {
				response.setMessage("payPwd is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getSecurityQuestion())
					&& StringUtils.isEmpty(regRequest
							.getOtherSecurityQuestion())) {
				response.setMessage("securityQuestion is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getSecurityAnswer())) {
				response.setMessage("securityAnswer is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getRealName())) {
				response.setMessage("realName is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getCertNo())) {
				response.setMessage("certNo is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getProfession())) {
				response.setMessage("profession is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getProv())
					|| StringUtils.isEmpty(regRequest.getCity())) {
				response.setMessage("prov city is null!");
				return;
			}
			if (StringUtils.isEmpty(regRequest.getAddress())) {
				response.setMessage("address is null!");
				return;
			}
			member = memberService.getObjectByName(regRequest.getLoginId());
			if (null == member) {
				response.setCode(InfoCode.MEMBER_NOT_EXIST);
				response.setMessage("member is null!");
				return;
			}
			if (!AccStatus.activate.equals(member.getStatus())) {
				response.setCode(InfoCode.MEMBER_REGED);
				response.setMessage("member had finished reg!");
				return;
			}
		}
		
		response.setCode(InfoCode.SUCCEED);
		/** 处理下一个任务 */
		chain.doHandler(chain, request, response);
		if (InfoCode.SUCCEED == response.getCode()) {
			response.setMessage("pay param pass! and " + response.getMessage());
		}
	}

}
